<template>
  <div class="login-background">
    <div class="login-container">
      <div class="login-box">
        <div class="login-header">
          <div class="logo">
            <svg viewBox="0 0 24 24" fill="currentColor" class="logo-icon">
              <path
                d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"
              />
            </svg>
          </div>
          <h1 class="login-title">后台管理系统</h1>
          <p class="login-subtitle">欢迎回来，请登录您的账户</p>
        </div>

        <form @submit.prevent="handleLogin" class="login-form">
          <div class="input-group">
            <label class="input-label">用户名</label>
            <div class="input-wrapper">
              <svg class="input-icon" viewBox="0 0 24 24" fill="currentColor">
                <path
                  d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"
                />
              </svg>
              <input
                v-model="username"
                type="text"
                placeholder="请输入用户名"
                class="login-input"
                autocomplete="username"
                required
              />
            </div>
          </div>

          <div class="input-group">
            <label class="input-label">密码</label>
            <div class="input-wrapper">
              <svg class="input-icon" viewBox="0 0 24 24" fill="currentColor">
                <path
                  d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z"
                />
              </svg>
              <input
                v-model="password"
                type="password"
                placeholder="请输入密码"
                class="login-input"
                autocomplete="current-password"
                required
              />
            </div>
          </div>

          <button type="submit" class="login-btn" :disabled="loading">
            <svg
              v-if="loading"
              class="loading-icon"
              viewBox="0 0 24 24"
              fill="currentColor"
            >
              <path
                d="M12 2A10 10 0 0 0 2 12a10 10 0 0 0 10 10 10 10 0 0 0 10-10A10 10 0 0 0 12 2zm0 18a8 8 0 0 1-8-8 8 8 0 0 1 8-8 8 8 0 0 1 8 8 8 8 0 0 1-8 8z"
              />
              <path
                d="M12 4a8 8 0 0 0-8 8 8 8 0 0 0 8 8 8 8 0 0 0 8-8 8 8 0 0 0-8-8zm0 14a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6z"
              />
            </svg>
            {{ loading ? "登录中..." : "登录" }}
          </button>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useUserStore } from "../store/user";

const username = ref("");
const password = ref("");
const loading = ref(false);
const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

async function handleLogin() {
  if (!username.value || !password.value) {
    alert("请输入账号和密码");
    return;
  }
  loading.value = true;
  try {
    const result = await userStore.login({
      username: username.value,
      password: password.value,
    });

    if (result.success) {
      console.log("登录成功，准备跳转");
      // 获取跳转路径：优先使用URL参数，其次使用store中记录的路径，最后使用首页
      const urlRedirect = route.query.redirect as string;
      const storeRedirect = userStore.getLastVisitedPath();
      const redirect = urlRedirect || storeRedirect || "/";

      console.log("跳转路径:", {
        urlRedirect,
        storeRedirect,
        finalRedirect: redirect,
      });

      // 清除记录的路径，避免影响后续导航
      localStorage.removeItem("lastVisitedPath");

      await router.replace(redirect);
    } else {
      alert(result.message || "登录失败");
    }
  } catch (e) {
    console.error("登录错误:", e);
    alert("登录请求失败");
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.login-background {
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  margin-bottom: 16px;
}

.logo-icon {
  width: 32px;
  height: 32px;
  color: white;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 8px 0;
  letter-spacing: -0.5px;
}

.login-subtitle {
  color: #718096;
  font-size: 16px;
  margin: 0 0 32px 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-label {
  font-weight: 600;
  color: #2d3748;
  font-size: 14px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 16px;
  width: 20px;
  height: 20px;
  color: #a0aec0;
  z-index: 1;
}

.login-input {
  width: 100%;
  padding: 16px 16px 16px 48px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  outline: none;
  transition: all 0.3s ease;
  background: white;
  color: #2d3748;
}

.login-input:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.login-input::placeholder {
  color: #a0aec0;
}

.login-btn {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.login-btn:active:not(:disabled) {
  transform: translateY(0);
}

.login-btn:disabled {
  background: #a0aec0;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.loading-icon {
  width: 20px;
  height: 20px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 平板端适配 */
@media (max-width: 768px) {
  .login-background {
    padding: 16px;
  }

  .login-box {
    padding: 32px 24px;
  }

  .login-title {
    font-size: 24px;
  }

  .login-subtitle {
    font-size: 14px;
  }

  .logo {
    width: 50px;
    height: 50px;
  }

  .logo-icon {
    width: 28px;
    height: 28px;
  }
}

/* 手机端适配 */
@media (max-width: 480px) {
  .login-background {
    padding: 12px;
  }

  .login-container {
    max-width: 100%;
  }

  .login-box {
    padding: 24px 20px;
    border-radius: 16px;
  }

  .login-title {
    font-size: 20px;
    margin-bottom: 6px;
  }

  .login-subtitle {
    font-size: 13px;
    margin-bottom: 24px;
  }

  .logo {
    width: 44px;
    height: 44px;
    margin-bottom: 12px;
  }

  .logo-icon {
    width: 24px;
    height: 24px;
  }

  .login-form {
    gap: 20px;
  }

  .input-label {
    font-size: 13px;
  }

  .login-input {
    padding: 14px 14px 14px 44px;
    font-size: 15px;
    border-radius: 10px;
  }

  .input-icon {
    left: 14px;
    width: 18px;
    height: 18px;
  }

  .login-btn {
    padding: 14px;
    font-size: 15px;
    border-radius: 10px;
  }

  .loading-icon {
    width: 18px;
    height: 18px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 360px) {
  .login-box {
    padding: 20px 16px;
  }

  .login-title {
    font-size: 18px;
  }

  .login-subtitle {
    font-size: 12px;
  }

  .logo {
    width: 40px;
    height: 40px;
  }

  .logo-icon {
    width: 20px;
    height: 20px;
  }

  .login-form {
    gap: 16px;
  }

  .login-input {
    padding: 12px 12px 12px 40px;
    font-size: 14px;
  }

  .input-icon {
    left: 12px;
    width: 16px;
    height: 16px;
  }

  .login-btn {
    padding: 12px;
    font-size: 14px;
  }
}

/* 横屏手机适配 */
@media (max-height: 500px) and (orientation: landscape) {
  .login-background {
    padding: 8px;
  }

  .login-box {
    padding: 20px;
  }

  .login-header {
    margin-bottom: 20px;
  }

  .logo {
    width: 40px;
    height: 40px;
    margin-bottom: 8px;
  }

  .login-title {
    font-size: 18px;
    margin-bottom: 4px;
  }

  .login-subtitle {
    font-size: 12px;
    margin-bottom: 20px;
  }

  .login-form {
    gap: 16px;
  }
}

body,
html {
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
</style>
